﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class SkillButton : MonoBehaviour, IPointerClickHandler
{
    // 绑定技能数据
    public SkillData skillData;

    private Image skillBtnImg;      // 技能图标的图片
    private Text skillLevelText;    //技能图标中的技能等级

    public void OnPointerClick(PointerEventData eventData)
    {
        SkillManager.Instance.SelectedSkill = skillData;
        SkillManager.Instance.DisplaySkill();
    }

    void Start()
    {
        skillBtnImg = GetComponent<Image>();
        skillLevelText = GetComponentInChildren<Text>();

        SkillManager.Instance.UpgradeSkillEvent += OnUpgradeSkillEvent;
    }

    // 更新技能时触发，进行技能图标中的等级更新
    void OnUpgradeSkillEvent(SkillData skillData)
    {
        if (this.skillData != null && this.skillData.Equals(skillData))
        {
            Debug.Log($"invoke OnUpgradeSkillEvent: {skillData.skillName}");
            skillLevelText.text = skillData.skillLevel.ToString();
        }
    }

    // 解锁技能，使技能图标颜色恢复正常，由外部调用
    public void UnlockSkill()
    {
        skillBtnImg.color = Color.white;
        skillData.isUnlocked = true;
    }
}
